package com.edu.scnu.element;

import java.awt.Graphics;
import java.util.Random;

import javax.swing.ImageIcon;

import com.edu.scnu.manager.ElementManager;
import com.edu.scnu.manager.GameElement;
import com.edu.scnu.manager.GameLoad;

public class Enemy extends ElementObj {
	private String type; // 种类
	private long filetime = 75;// 控制子弹间隔时间
	private String Movefx;// 移动方向，只有left和right
	Random rand = new Random();

	@Override
	public void showElement(Graphics g) {
		// TODO 自动生成的方法存根
		if (!this.isIshow()) {
			return;
		}
		g.drawImage(this.getIcon().getImage(), this.getX(), this.getY(), this.getW(), this.getH(), null);
	}

	@Override
	public ElementObj createElement(String str) {
		// str格式：x,y,类型，时间
		String[] split = str.split(",");
		this.setX(Integer.parseInt(split[0]));
		this.setY(Integer.parseInt(split[1]));
		this.type = split[2];
		this.setBornTime(Long.parseLong(split[3]));
		ImageIcon icon2 = GameLoad.imgMap.get("enemy" + this.type);// 这里传入的是配置文件中=号前的字符串
		this.setW(icon2.getIconWidth());
		this.setH(icon2.getIconHeight());
		this.setIcon(icon2);
		if (this.getX() >= 300) {
			this.Movefx = "left";
		} else {
			this.Movefx = "right";
		}
		this.setAttack(1);
		this.setHp(1);
		return this;
	}

	/**
	 * @说明 敌人的移动逻辑
	 */
	@Override
	protected void move(long gameTime) {
		if (this.getX() >= 1100 - this.getX()) {
			this.Movefx = "left";
		}
		if (this.getX() <= 0) {
			this.Movefx = "right";
		}
		if (this.Movefx.equals("right")) {
			this.setX(this.getX() + 3);
		}
		if (this.Movefx.equals("left")) {
			this.setX(this.getX() - 3);
		}
		if (gameTime > this.getBornTime() + 300) {
			this.setY(this.getY() + 1);
		}

		if (this.getY() >= 800 - this.getH()) {
			this.setLive(false);
			this.setCanRmove(true);
		}
	}

	/**
	 * @说明 敌人状态改变时图片的刷新
	 */
	@Override

	public void updateImage(long gameTime) {
		if (gameTime < this.getBornTime()) {
			this.setIshow(false);
		} else {
			this.setIshow(true);
		}
		this.setIcon(GameLoad.imgMap.get("enemy" + this.type));// 这里传入的是配置文件中=号前的字符串
	}

	/**
	 * @说明 敌人的添加子弹
	 */
	@Override
	protected void add(long gameTime) {
		if (gameTime % filetime == 0) {
			ElementObj obj = GameLoad.getObj("enemyfile");
			ElementObj element = obj.createElement(this.getFile()); // 以后的框架学习中会碰到
			// 会帮助返回对象的实体,并初始化数据
			ElementManager.getManager().addElement(element, GameElement.ENEMYFILE);
			// 如果要控制子弹速度等，还需要代码编写
		}

	}

	/**
	 * @author 14737
	 * @说明 用于获取敌人的位置信息，用于生成子弹
	 * @需要解决的问题 传输格式"x:${横坐标},y:${纵坐标},n:${子弹图片的键，具体参考GameData.pro文件,例“fire”}"
	 * @return
	 */
	public String getFile() { // 定义的方法
		int x = this.getX();
		int y = this.getY();
		x += this.getW() / 2 - 5;
		y += this.getH() - 5;
		return "x:" + x + ",y:" + y + ",n:" + "efire" + this.type;
	}

	@Override
	public void die(long gameTime) {
		Random r = new Random();
		int n = r.nextInt(100);
		if (n <= 50) {
			ElementObj item = new GameItem().createElement(this.getX() + "," + this.getY() + "," + (rand.nextInt(7) + 1));
			ElementManager.getManager().addElement(item, GameElement.GAMEITEM);
		}
	}

	@Override
	public void beAttacked(int attack) {
		this.setHp(this.getHp() - attack);
		if (this.getHp() <= 0) {
			super.beAttacked(attack);
		}
	}
}